package cn.weiyang.controller;

import cn.weiyang.model.OrderRuselt;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 * Created by Administrator on 2017/7/25.
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static Logger logger= LogManager.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(Exception.class)
    public OrderRuselt<?> handleException(Exception e){
        logger.info("系统繁忙");
        OrderRuselt<Void> or=new OrderRuselt<Void>();
        or.setCode(Errcode.Gl_SYS_BUSY.code);
        or.setMsg(Errcode.Gl_SYS_BUSY.msg);
        logger.error(e.getMessage());
        return or;
    }


    @ExceptionHandler(NullPointerException.class)
    public OrderRuselt<?> handleException1(){
        logger.info("空指针异常");
        OrderRuselt<Void> or=new OrderRuselt<Void>();
        or.setCode(Errcode.Gl_SYS_NULL.code);
        or.setMsg(Errcode.Gl_SYS_NULL.msg);
        return or;
    }


    @ExceptionHandler(NumberFormatException.class)
    public OrderRuselt<?> handleException2(){
        logger.info("数字格式化异常");
        OrderRuselt<Void> or=new OrderRuselt<Void>();
        or.setCode(Errcode.Gl_SYS_FORMAT_ERR.code);
        or.setMsg(Errcode.Gl_SYS_FORMAT_ERR.msg);
        return or;
    }


    @ExceptionHandler(MissingServletRequestParameterException.class)
    public OrderRuselt<?> handleException3(){
        logger.info("参数校验异常");
        OrderRuselt<Void> or=new OrderRuselt<Void>();
        or.setCode(Errcode.Gl_SYS_ARG_INVALID.code);
        or.setMsg(Errcode.Gl_SYS_ARG_INVALID.msg);
        return or;
    }

    @ExceptionHandler(ServiceException.class)
    public OrderRuselt<?> handleException4(ServiceException se){
        OrderRuselt<Void> or=new OrderRuselt<Void>();
        or.setCode(se.getErrcode().code);
        or.setMsg(se.getErrcode().msg);
        logger.info(se.getErrcode().msg);
        return or;
    }
}
